package trading_strategy.events.filtered;

import trading_strategy.events.AbstractMarketEvent;
import trading_strategy.events.OrderBookUpdate;
import trading_strategy.events.PublicTradeUpdate;
import trading_strategy.instruments.ListedInstrument;

/**
 * Created by IntelliJ IDEA.
 * User: gderoujoux
 * Date: 4 juin 2010
 * Time: 13:18:25
 * To change this template use File | Settings | File Templates.
 */
public class ImprovedCandleFilter extends CandleFilter {
    OrderBookUpdate lastOrderBook;
    int askVol;
    int bidVol;

    public ImprovedCandleFilter(ListedInstrument instrument, String name, int durationInSeconds) {
        super(instrument, name, durationInSeconds);
    }

    @Override
    public void onMarketEvent(AbstractMarketEvent event) {
        super.onMarketEvent(event);
        if (event instanceof OrderBookUpdate) {
            lastOrderBook = (OrderBookUpdate) event;
        } else if (event instanceof PublicTradeUpdate) {
            PublicTradeUpdate trade = (PublicTradeUpdate) event;
            if (trade.getSide() == PublicTradeUpdate.TradeSide.BUY) {
                askVol += trade.getQty();
            } else if (trade.getSide() == PublicTradeUpdate.TradeSide.SELL) {
                bidVol += trade.getQty();
            }
        }
    }

    @Override
    protected void sendCurrentCandle(AbstractMarketEvent event) {
        if (currentCandle != null && event.getDate().after(currentCandle.end)) {
            currentCandle.askTradedQty = askVol;
            currentCandle.bidTradedQty = bidVol;
            super.sendCurrentCandle(event);    //To change body of overridden methods use File | Settings | File Templates.
            askVol = 0;
            bidVol = 0;

        }
    }
}
